/**
 * 
 */
package br.com.cnpv.modelo.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IAssociacaoDAO;
import br.com.cnpv.modelo.entity.Associacao;

/**
 * @author rmsilva
 *
 */
public class AssociacaoDAO implements IAssociacaoDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Associacao> buscar(Associacao associacao, Session session) throws HibernateException {
		
		String clausula = "";
		Query query = null;
		
		if(associacao.getCdPess() != null && StringUtils.isNotEmpty(associacao.getCdPess())){
		
			clausula = associacao.getCdPess();
			
			query = session.getNamedQuery("Associacao.buscarByCdPess");

		}else{
			
			clausula = "%" + associacao.getRazaoSocial() + "%";
			query = session.getNamedQuery("Associacao.buscarByRazSoc");
		}
		
		query.setString("clausula", clausula);
		
		return query.list();
	}

	public void saveOrUpdate(Associacao associacao, Session session)
			throws HibernateException {
		session.merge(associacao);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Associacao> listar(Session session) throws HibernateException 
	{
		Query query = session.getNamedQuery("Associacao.listar");
		return query.list();
	}

}
